package com.example.casper.debugapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ImageViewMainActivity extends AppCompatActivity {

    private Button buttonPrevious, buttonNext;//只声明变量
    private ImageView imageViewFunny;
    private int[] imageIDArray;
    private int imageIDArrayCurrentIndex;

    public ImageViewMainActivity() {//第一处错误
        imageIDArrayCurrentIndex = 0;
        imageIDArray = new int[]{R.drawable.funny_1, R.drawable.funny_2
        , R.drawable.funny_3, R.drawable.funny_4, R.drawable.funny_5
        , R.drawable.funny_6
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view_main);//初始化只能在setContentView后面


        buttonPrevious = (Button)findViewById(R.id.button_previous);//第二处错误
        buttonNext =(Button) findViewById(R.id.button_next);//第三处错误

        imageViewFunny = (ImageView)findViewById(R.id.image_view_funny);//111

        buttonPrevious.setOnClickListener(new MyButtonClickListener());
        buttonNext.setOnClickListener(new MyButtonClickListener());

    }

    private class MyButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if ((((Button) view).getText()).equals( "下一个")) {
                imageIDArrayCurrentIndex ++;
            } else {
                imageIDArrayCurrentIndex --;
            }
            if(imageIDArrayCurrentIndex<0){
                imageIDArrayCurrentIndex=imageIDArray.length-1;
            }
            if(imageIDArrayCurrentIndex==imageIDArray.length){
                imageIDArrayCurrentIndex=0;
            }
            imageViewFunny.setImageResource(imageIDArray[imageIDArrayCurrentIndex]);//数组越界
        }
    }
}
